define(function(require){
    var main =require('/static/main.js');
    require('/static/ajaxfileupload.js');
    var m = new main();
    function Cont(){
        this.Fun._init();
    }
    Cont.prototype.Fun = {
        MytipLevelId : '',
        EntMytipId : 0,
        ParentId : 0,
        _init:function(){
            this.getlist();
            this.bindEvent();
            this.changeHead();
        },
        bindEvent:function(){
            var _this = this;
            // 点击修改信息 + 添加按钮
            $('.add').click(function(){
                var $this = $(this);
                var data = {
                    LevelName:$('.LevelName').val().trim(),
                    ParentLevelId:$('#ParentLevelId option:selected').val(),
                    PerformanceRatio :$("#AddEmployee .PerformanceRatio").val(),
                    BrokerageRatio:$("#AddEmployee .BrokerageRatio").val()
                }
                if(data.LevelName.length < 1){
                    layer.msg('请输入等级名称');
                    return false;
                }
                if(data.ParentLevelId.length < 1){
                    layer.msg('请选择上级');
                    return false;
                }
                if(data.PerformanceRatio.length < 1){
                    layer.msg('请输入销售绩效比例');
                    return false;
                }
                if(data.BrokerageRatio.length < 1){
                    layer.msg('请输入绩效佣金比例');
                    return false;
                }

                if(_this.MytipLevelId > 0){
                    data.MytipLevelId = _this.MytipLevelId;
                }
                $.ajax({
                    type: "POST",
                    url: "/employee/enterprise-save-level",
                    data:data,
                    dataType: "json",
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            if(_this.MytipLevelId){
                                layer.msg('修改成功');
                            }else{
                                layer.msg('添加成功');
                            }
                            $('#AddEmployee').modal('hide');
                            _this.reset(100);
                            var table = $('#datatable').DataTable();
                            table.ajax.reload();
                        }else{
                            layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                });
            })

            $('#AddEmployee').on('hide.bs.modal', function () {
                _this.reset(100);
            })
        },
        getlist:function(){
            var _this = this;
            var emdataa = [];
            var param = {};
            var sort = 0;
            var table = $('#datatable').dataTable({
                'ordering':false,
                "serverSide": true,
                "searching":false,
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": true,
                "autoWidth": false,
                ajax: function (data, callback, settings) {
                    //封装请求参数
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    sort = (param.PageNo-1)*param.PageSize;
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/employee/get-enterprise-employee-level-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            emdataa = result.data;
                            callback(returnData);
                            var str = '<option value="">请选择上级</option>';
                            $.each(result.data,function(k,v){
                                str += '<option value="'+ v.MytipLevelId+'">'+ v.LevelName+'</option>';
                            });
                            $("#ParentLevelId").html(str);
                        }
                    });
                },
                "aoColumns":[
                    {"mDataProp": 'LevelName'},
                    {"mDataProp": 'ParentLeveName'},
                    {"mDataProp": 'PerformanceRatio'},
                    {"mDataProp": 'BrokerageRatio'},
                    {"mDataProp": 'CreateTime',
                        render:function(data){
                            return m.formatTimeAll2(data);
                        }
                    },
                    {
                        "mDataProp": "MytipLevelId",
                        "sClass": "option_btn",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            // console.log(arguments);
                            var str = '<span class="btn_detial EditBtn" data-id="'+sData+'">编辑</span>';
                            if(oData.MytipLevelType == 200){
                                str += '<span class="btn_del DelBtn"  data-id="'+sData+'">删除</span>';
                            }
                            $(nTd).html(str);
                        }
                    }
                ]
            }).api();

            // 点击搜索
            $('#btnSearch').click(function(){
                param.SearchLevelName = $('.SearchLevelName').val();
                var table = $('#datatable').DataTable();
                table.ajax.reload();

            });
            // 点击重置按钮
            $('#btnRefresh').click(function(){
                $('.SearchLevelName').val('');
                param.SearchLevelName = '';
                var table = $('#datatable').DataTable();
                table.ajax.reload();
            });

            //编辑
            $(document).on('click','.EditBtn',function(){
                var MytipLevelId = parseInt($(this).attr("data-id"));
                $.each(emdataa,function(k,v){
                    if(v.MytipLevelId == MytipLevelId){
                        $("#AddEmployee .LevelName").val(v.LevelName);
                        $("#AddEmployee .PerformanceRatio").val(v.PerformanceRatio);
                        $("#AddEmployee .BrokerageRatio").val(v.BrokerageRatio);
                        $("#ParentLevelId").val(v.ParentLevelId);
                        _this.MytipLevelId = v.MytipLevelId;
                        getopcion(k,v.ParentLevelId);
                        $("#AddEmployee .modal-title").html('编辑等级信息');
                    }
                });
                $("#AddEmployee").modal('show');
            });
            // 删除
            var isactive = false;
            $(document).on('click','.DelBtn',function(){
                if(isactive == true){
                    return false;
                }
                var id = $(this).attr('data-id');
                isactive = true;
                $.ajax({
                    type: "POST",
                    url: "/employee/enterprise-employee-level-del",
                    data :{MytipLevelId:id},
                    dataType: "json",
                    success: function (ret){
                        isactive = false;
                        if(ret.IsBizSuccess){
                            layer.msg("删除成功");
                            var table1 = $('#datatable').DataTable();
                            table1.ajax.reload();
                        }else{
                            layer.msg(ret.ErrMsg);
                        }
                    }
                });
            });


            //设为超级管理员
            var issupper = false;
            $(document).on('click','.SetSupper',function(){
                if(isactive == true){
                    return false;
                }
                var id = $(this).attr('data-id');
                layer.confirm('确定转移超级账户至当前所选账号吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    issupper = true;
                    $.ajax({
                        type: "POST",
                        url: "/enterprise/enterprise-set-super",
                        data :{EntMytipId:id},
                        dataType: "json",
                        success: function (ret){
                            issupper = false;
                            if(ret.IsBizSuccess){
                                layer.msg("成功更改超级管理员");
                                var table1 = $('#datatable').DataTable();
                                table1.ajax.reload();
                            }else{
                                layer.msg(ret.ErrMsg);
                            }
                        }
                    });
                });
            });


            function getopcion(index,levelid){
                if(typeof(index) != 'undefined'){
                    var str = '<option value="">请选择上级</option>';
                    $.each(emdataa,function(k,v){
                        if(k != index){
                            str += '<option value="'+ v.MytipLevelId+'" '+(levelid == v.MytipLevelId ? 'selected':'' )+'>'+ v.LevelName+'</option>';
                        }
                    });
                    $("#ParentLevelId").html(str);
                }else{
                    $("#ParentLevelId").closest(".form-group").show();
                    var str = '<option value="">请选择上级</option>';
                    $.each(emdataa,function(k,v){
                        str += '<option value="'+ v.MytipLevelId+'">'+ v.LevelName+'</option>';
                    });
                    $("#ParentLevelId").html(str);
                }
            }
        },
        reset:function(type){
            if(type == 100){
                $("#Photo").attr("src",'');
                $("#Photo").attr("data-src",'');
                $("#AddEmployee .LevelName").val('');
                $("#ParentLevelId").val('');
                $("#AddEmployee .PerformanceRatio").val('');
                $("#AddEmployee .BrokerageRatio").val('');
                this.EntMytipId = 0;
                $("#AddEmployee .modal-title").html('添加等级');
            }else{

            }
        },
        changeHead:function(){
            var _this = this;
            //图片上传
            $("#file").on("change",function () {
                ajaxFileUpload();
            });
            function ajaxFileUpload(){
                $.ajaxFileUpload({
                    url:m.baseUrl+"/customer/edit-head",
                    secureuri: false,
                    fileElementId: 'file',
                    dataType: 'json',
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            $(".UserPhoto").attr({"src":ret.Photo,'data-src':ret.FileName})
                        }else{
                            layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                })
                return false;
            }
        },
    }
    var a = new Cont();

})